package com.hl.web.fore;

import com.hl.pojo.Consultant;
import com.hl.service.ConsultantService;
import com.hl.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consultant")
public class ConsultantController {
    @Autowired
    private ConsultantService consultantService;
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Consultant consultant) {
        consultantService.add(consultant);
        return ResultEntity.ok();
    }

    @GetMapping("/findAll")
    public ResultEntity findAll() {
        return ResultEntity.ok(consultantService.findAll());
    }

    @GetMapping("/find")
    public ResultEntity find(String shanchang){
        System.out.println(shanchang);
        Map<Integer,Integer> mp = new HashMap<>();
        String[] strings = null;
        if(shanchang!=null && !shanchang.equals("")){
            strings = shanchang.split(",");
            for(int i=0;i<strings.length;i++){
                mp.put(Integer.parseInt(strings[i]),1);
            }
        }
        List<Consultant> list = consultantService.findAll();
        for(int i=0;i<list.size();i++){
            boolean pd = false;
            if(shanchang!=null && !shanchang.equals("")){
                for(int j=0;j<list.get(i).getConsultantExcellences().size();j++){
                    if ( mp.get(list.get(i).getConsultantExcellences().get(j).getFieldOfExcellence().getId())!=null){
                        pd=true;
                        break;
                    }
                }
                if(!pd){
                    list.remove(i);
                    i--;
                }
            }
        }
        return ResultEntity.ok(list);
    }

    @GetMapping("/del")
    public ResultEntity del(Integer id) {
        consultantService.del(id);
        return ResultEntity.ok();
    }
    @GetMapping("/findById")
    public ResultEntity findById(Integer id) {
        return ResultEntity.ok(consultantService.findById(id));
    }
    @PostMapping("/update")
    public ResultEntity update(@RequestBody Consultant consultant) {
        consultantService.update(consultant);
        return ResultEntity.ok();
    }
    @GetMapping("/findByExcellenceAndLevel")
    public ResultEntity findByExcellenceAndLevel(Integer consultantLevelId,Integer fieldNameId) {
        return ResultEntity.ok(consultantService.findByExcellenceAndLevel(consultantLevelId,fieldNameId));
    }

    @GetMapping("/findByName")
    public ResultEntity findByName(String name){
        List<Consultant> list = consultantService.findAll();

        for(int i=0;i<list.size();i++){
            boolean pd = false;
            for(int j=0;j<list.get(i).getConsultantExcellences().size();j++){
                if(list.get(i).getConsultantExcellences().get(j).getFieldOfExcellence().getFieldName().contains(name))pd=true;
            }
            if(list.get(i).getConsultantName().contains(name))pd=true;
            if(!pd){
                list.remove(i);
                i--;
            }
        }
        return ResultEntity.ok(list);
    }
}
